/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Util;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URLEncoder;

/**
 *
 * @author Wolfgang
 */
public class CommunicateUtil {

    public static boolean startRace(int raceId) {
        String retval = "";
        try {
            java.net.URL url = new java.net.URL("http://10.0.0.82:4711");
            HttpURLConnection request = (HttpURLConnection) url.openConnection();
            request.setRequestMethod("POST");
            request.setDoOutput(true);
            request.connect();
            DataOutputStream out = new DataOutputStream(request.getOutputStream());
            String content = "command=" + URLEncoder.encode("start");
            out.writeBytes(content);
            out.flush();
            out.close();
            DataInputStream in = new DataInputStream(request.getInputStream());
            System.out.println(in.readLine());
            in.close();
            // Start Game on Second Py
            url = new java.net.URL("http://10.0.0.83:4711");
            request = (HttpURLConnection) url.openConnection();
            request.setRequestMethod("POST");
            request.setDoOutput(true);
            request.connect();
            out = new DataOutputStream(request.getOutputStream());
            out.writeBytes(content);
            out.flush();
            out.close();
            in = new DataInputStream(request.getInputStream());
            System.out.println(in.readLine());
            in.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public static boolean stopRace() {
        String retval = "";
        try {
            java.net.URL url = new java.net.URL("http://10.0.0.82:4711");
            HttpURLConnection request = (HttpURLConnection) url.openConnection();
            request.setRequestMethod("POST");
            request.setDoOutput(true);
            request.connect();
            DataOutputStream out = new DataOutputStream(request.getOutputStream());
            String content = "command=" + URLEncoder.encode("stop");
            out.writeBytes(content);
            out.flush();
            out.close();
            DataInputStream in = new DataInputStream(request.getInputStream());
            System.out.println(in.readLine());
            in.close();
            // Stop Second Raspberry
            url = new java.net.URL("http://10.0.0.83:4711");
            request = (HttpURLConnection) url.openConnection();
            request.setRequestMethod("POST");
            request.setDoOutput(true);
            request.connect();
            out = new DataOutputStream(request.getOutputStream());
            out.writeBytes(content);
            out.flush();
            out.close();
             in = new DataInputStream(request.getInputStream());
            System.out.println(in.readLine());
            in.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public static String readTagId() {
        try {
            java.net.URL url = new java.net.URL("http://10.0.0.81:4711");
            HttpURLConnection request = (HttpURLConnection) url.openConnection();
            request.setRequestMethod("GET");
            request.connect();
            java.io.BufferedReader rd = new java.io.BufferedReader(new java.io.InputStreamReader(request.getInputStream()));
            return rd.readLine();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static boolean checkRaspberry1(){
        return true;
    }
}
